var set_count = null;

function AddSet() {
  var table = document.getElementById('sets');
  if (table.rows.length > 10) {
    return;
  }
  if (set_count == null) {
    set_count = table.rows.length - 1;
  }

  var index = set_count;
  var index_str = index.toString();
  html = ['<td>Set Name: ',
          '<input name=set', index_str, '_n value="Custom Set ',
          (index + 1).toString(), '"/> &nbsp; ',
          '<input type=button onclick=AddCard(', index_str,
          ') value="Add Card"> &nbsp; ',
          '<input type=button onclick=RemoveSet(', index_str,
          ') value="Remove Set"/>',
          '<table border=1 id=set', index_str,
          '_cards next_card=1 class=card_input>',
          '<tr><th rowspan=2 valign=bottom style=min-width:15em>Card Name</th>',
          '<th rowspan=2 valign=bottom style=min-width:3.5em>Cost</th>',
          '<th colspan=7>Type</th><th colspan=3>Plus</th>',
          '<th rowspan=2>Remove Card</th></tr>',
          '<tr><th>Action</th><th>Attack</th><th>Defense</th><th>Duration</th>',
          '<th>Reaction</th><th>Treasure</th><th>Victory</th><th>Actions</th>',
          '<th>Buys</th><th>Cards</th></tr>',
          '</table>',
          '<br>',
          '</td>'];
  var row = table.insertRow(table.rows.length - 1);
  row.id = 'set' + index_str;
  row.innerHTML = html.join('')

  set_count += 1;
}

function RemoveSet(index) {
  var table = document.getElementById('sets');
  var row = document.getElementById('set' + index.toString());
  table.deleteRow(row.rowIndex);
}

function AddCard(set_index) {
  var table = document.getElementById('set' + set_index.toString() + '_cards');
  var set_index_str = set_index.toString();
  var card_index_str = table.getAttribute('next_card');
  var id_base = 'card' + set_index_str + '_' + card_index_str;

  html = ['<td><input class=full_width name=', id_base, '_n value="Card ',
          card_index_str, '"/></td>',
          '<td><select class=full_width name=', id_base, '_c>',
          '<option value=0>0</option>',
          '<option value=1>1</option>',
          '<option value=2>2</option>',
          '<option value=3>3</option>',
          '<option value=4>4</option>',
          '<option value=5>5</option>',
          '<option value=6>6</option>',
          '<option value=7>7</option>',
          '<option value=8>8</option>',
          '<option value=9>9</option>',
          '<option value=10>10</option>',
          '<option value=11>11</option>',
          '<option value=12>12</option>',
          '<option value=13>13</option>',
          '<option value=14>14</option>',
          '<option value=99>15+</option>',
          '</select></td>',
          '<td><input type=checkbox name=', id_base, '_ta/></td>',
          '<td><input type=checkbox name=', id_base, '_tb/></td>',
          '<td><input type=checkbox name=', id_base, '_td/></td>',
          '<td><input type=checkbox name=', id_base, '_te/></td>',
          '<td><input type=checkbox name=', id_base, '_tr/></td>',
          '<td><input type=checkbox name=', id_base, '_tt/></td>',
          '<td><input type=checkbox name=', id_base, '_tv/></td>',
          '<td><input type=checkbox name=', id_base, '_pa/></td>',
          '<td><input type=checkbox name=', id_base, '_pb/></td>',
          '<td><input type=checkbox name=', id_base, '_pc/></td>',
          '<td><input type=button onclick=RemoveCard(', set_index_str, ',',
          card_index_str, ') value="Remove"/></td>',
         ];

  var row = table.insertRow(table.rows.length);
  row.id = id_base;
  row.className = 'card_input';
  row.innerHTML = html.join('');
  table.setAttribute('next_card', parseInt(card_index_str) + 1);
}

function RemoveCard(set_index, card_index) {
  var table = document.getElementById('set' + set_index.toString() + '_cards');
  var row = document.getElementById('card' + set_index.toString() + '_' +
                                    card_index.toString());
  table.deleteRow(row.rowIndex);
}
